<?php
namespace app\common\exception;

use think\exception\Handle;
use think\Request;
use think\exception\HttpException;
use think\exception\ErrorException;
use RuntimeException;

class Http extends Handle
{

    //code = 1 用户名为空
    //code = -89 旅行币为负
    //code = 800 检查token失败
    //code = 801 token为空
    //code = -87 货币类型错误

    public function render(\Exception $e)
    {

        if ($e instanceof \Exception) {
            if ($e->getCode() == -89) {
                if (Request::instance()->isAjax()) {
                    return json(['code' => $e->getCode(), 'message' => '余额不足']); 
                }else{
                    return json(['code' => $e->getCode(), 'message' => '余额不足']); 
                }       
            }
        }elseif (1) {
            return parent::render($e);
            # code...
        }
        if ($e instanceof HttpException) {
            $statusCode = $e->getStatusCode();
            $message = $e->getMessage();
            return json(['code' => $statusCode, 'message' => $message]);
            if (Request::instance()->isAjax()) {
                return json(['code' => $statusCode, 'message' => $message]);
            }else{
                return view('exceptions/'.$statusCode, ['code' => $statusCode, 'message' => $message]);
            }
        }elseif ($e instanceof ErrorException) {
            return ['code', 'message'];
        }elseif ($e instanceof RuntimeException) {
            return json(['code' => $e->getCode(), 'message' => $e->getMessage()]);
        }else{
            return json(['code' => $e->getCode(), 'message' => $e->getMessage()]);
        }
        
    }

}